

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;



class JNIOpenCV {
    static {
        System.loadLibrary("JNI2OpenCV");
    }
    public native int[] detectFace(int minFaceWidth, int minFaceHeight, String cascade, String filename);
}

public class FaceDetection {
	private JNIOpenCV myJNIOpenCV;
	private FaceDetection myFaceDetection;
	private int[] rect  = null;
	private int nrFace = 0;
	
	
	public int[] getRect() {
		return rect;
	}

	public void setRect(int[] rect) {
		this.rect = rect;
	}

	public int getNrFace() {
		return nrFace;
	}

	public void setNrFace(int nrFace) {
		this.nrFace = nrFace;
	}

	public FaceDetection() throws IOException {
		myJNIOpenCV = new JNIOpenCV();
		String filename = "lena.jpg";
		String cascade = "haarcascade_frontalface_alt.xml";
	
		String string_Url = "http://192.168.178.50/test.jpg";
		
		BufferedImage img = null;
		 try {
		     img = ImageIO.read(new URL(string_Url));
		 } catch (IOException e) {
		 }
		
		 try {
			    File outputfile = new File("saved.jpg");
			    ImageIO.write(img, "jpg", outputfile);
			} catch (IOException e) {
			   // ...
			}
		
		int[] detectedFaces = myJNIOpenCV.detectFace(200, 350, cascade, "saved.jpg");
		int numFaces = detectedFaces.length / 4;
		this.nrFace = numFaces;
    	//System.out.println("numFaces = " + numFaces);
    	for (int i = 0; i < numFaces; i++) {
    		this.rect= detectedFaces;

    		System.out.println("Face " + i + ": " + detectedFaces[4 * i + 0] + " " + detectedFaces[4 * i + 1] + " " + detectedFaces[4 * i + 2] + " " + detectedFaces[4 * i + 3]);
    	}
	}
    
    public static void main(String args[]) throws IOException {
        FaceDetection myFaceDetection = new FaceDetection();   
    }
}